<?php

class DBMLogicalFilterTranslator implements DBMTranslator {
	private static $translationDictionary = array(
		DBManager::TYPE_MSSQL => array(
			DBMLogicalFilter::EQUALS => "%s = %s",
			DBMLogicalFilter::NOT_EQUALS => "%s != %s", 
			DBMLogicalFilter::BIGGER => "%s > %s", 
			DBMLogicalFilter::BIGGER_OR_EQ => "%s >= %s", 
			DBMLogicalFilter::SMALLER => "%s < %s", 
			DBMLogicalFilter::SMALLER_OR_EQ  => "%s <= %s"  
		), 
		DBManager::TYPE_MYSQL => array(
			DBMLogicalFilter::EQUALS => "%s = %s",
			DBMLogicalFilter::NOT_EQUALS => "%s != %s", 
			DBMLogicalFilter::BIGGER => "%s > %s", 
			DBMLogicalFilter::BIGGER_OR_EQ => "%s >= %s", 
			DBMLogicalFilter::SMALLER => "%s < %s", 
			DBMLogicalFilter::SMALLER_OR_EQ  => "%s <= %s"  
		), 
		DBManager::TYPE_POSTGRESQL => array(
			DBMLogicalFilter::EQUALS => "%s = %s",
			DBMLogicalFilter::NOT_EQUALS => "%s != %s", 
			DBMLogicalFilter::BIGGER => "%s > %s", 
			DBMLogicalFilter::BIGGER_OR_EQ => "%s >= %s", 
			DBMLogicalFilter::SMALLER => "%s < %s", 
			DBMLogicalFilter::SMALLER_OR_EQ  => "%s <= %s" 
		)
	);
	
	public static function translateToSQLServer($filter, $command = null){
		// get the dictionary
		$dictionary = self::$translationDictionary;
		
		// get the operation
		$operation = $filter->getLogicalOperation();
		
		// format the data
		$property 		= $filter->getProperty();
		$expectedValue	= $filter->getExpectedValue();
		
		// format the expected value
		$expectedValue = DBMGeneralTranslator::translateToSQLServer($expectedValue, $command);
		
		// format and return
		return sprintf($dictionary[DBManager::TYPE_MSSQL][$operation], $property, $expectedValue);
	}
	public static function translateToMySQL($filter, $command = null){
		// get the dictionary
		$dictionary = self::$translationDictionary;
		
		// get the operation
		$operation = $filter->getLogicalOperation();
		
		// format the data
		$property 		= $filter->getProperty();
		$expectedValue	= $filter->getExpectedValue();
		
		// format the expected value
		$expectedValue = DBMGeneralTranslator::translateToSQLServer($expectedValue, $command);
		
		// format and return
		return sprintf($dictionary[DBManager::TYPE_MYSQL][$operation], $property, $expectedValue);
	}
	public static function translateToPostgreSQL($filter, $command = null){
		// get the dictionary
		$dictionary = self::$translationDictionary;
		
		// get the operation
		$operation = $filter->getLogicalOperation();
		
		// format the data
		$property 		= $filter->getProperty();
		$expectedValue	= $filter->getExpectedValue();
		
	 	// format the expected value
		$expectedValue = DBMGeneralTranslator::translateToSQLServer($expectedValue, $command);
		
		// format and return
		return sprintf($dictionary[DBManager::TYPE_POSTGRESQL][$operation], $property, $expectedValue);
	}
}
?>